package com.apes.exception;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import javax.persistence.*;
import java.util.*;


/**
 * 功能：订阅者
 *
 * @author xul
 * @create 2019-10-24 10:57
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = MessageSubscriber.CONST_TABLE_NAME)
public class MessageSubscriber extends Model {
    public final static String CONST_TABLE_NAME = "message_subscriber";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "消息订阅者")
    private String id;

    @Column(name = "name", length = 128)
    @Comment("描述")
    private String name;

    @States(name = "订阅类型", values = {
            @State(id = "C", name = "普通"),
            @State(id = "W", name = "web hook")})
    @Column(name = "subscription_type", length = 64, nullable = false)
    @Comment("订阅类型")
    private String subscriptionType;

    @Column(name = "web_hook", length = 256)
    @Comment("web hook地址")
    private String webHook;

    @Column(name = "secret", length = 100)
    @Comment("密钥")
    private String secret;

    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("是否有效")
    private boolean valid = true;

    @OneToMany(mappedBy = "messageSubscriber", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<MessageTopic> messageTopics;

    public List<MessageTopic> getMessageTopics() {
        if (messageTopics==null) return new ArrayList<>();
        for (MessageTopic topic : messageTopics) {
            topic.setMessageSubscriber(this);
        }
        return messageTopics;
    }

    @Transient
    private List<MessageTopic> outBoxTopics;

    @Transient
    private List<MessageTopic> MQTopics;

    @Transient
    private List<MessageTopic> ESBTopics;

}
